package P109_ReflexEfficiency;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

class Utils {
    //获得一个泛型类的实际类型
    public static <T> Class<T> getGenricClassType(Class clz) {
        Type type = clz.getGenericSuperclass();
        if (type instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) type;
            Type[] types = pt.getActualTypeArguments();
            if (types.length > 0 && types[0] instanceof Class) {
                //若有多个泛型参数,依据索引位置返回
                return (Class<T>) types[0];
            }
        }
        return (Class<T>) Object.class;
    }
}